# -*- coding: utf-8 -*-
# @Time    : 2020/2/26 下午1:48
# @Author  : lilong
# @File    : exp2.py

import os
from models.adaptation_mode.exp2.exp21 import *


class IBook():
    """电子书文档的接口类"""
    def parseFile(self, filePath):
        pass

    def getCatalogue(self):
        pass

    def getPageCount(self):
        pass

    def getPage(self, pageNum):
        pass


class EpubBook(IBook):
    """TXT解析类：
        实现IBook接口
    """
    def parseFile(self, filePath):
        # 模拟文档的解析
        print(filePath + " 文件解析成功")
        self.__pageCount = 800
        return True

    def getCatalogue(self):
        return Catalogue("Epub电子书")

    def getPageCount(self):
        return self.__pageCount

    def getPage(self, pageNum):
        return Page(pageNum)


class TxtBook(IBook):
    """TXT解析类：
        实现IBook接口
    """
    def parseFile(self, filePath):
        # 模拟文档的解析
        print(filePath + " 文件解析成功")
        self.__pageCount = 500
        return True

    def getCatalogue(self):
        return Catalogue("TXT电子书")

    def getPageCount(self):
        return self.__pageCount

    def getPage(self, pageNum):
        return Page(pageNum)


class Reader():
    """阅读器：核心类"""
    
    def __init__(self, name):
        self.__name = name
        self.__filePath = ""
        self.__curBook = None
        self.__curPageNum = -1

    def __initBook(self, filePath):
        # 初始化书籍方法
        self.__filePath = filePath
        extName = os.path.splitext(filePath)[1]
        if (extName.lower() == ".epub"):
            self.__curBook = EpubBook()
        elif (extName.lower() == ".txt"):
            self.__curBook = TxtBook()
        elif (extName.lower() == ".pdf"):
            self.__curBook = PdfAdapterBook()
        else:
            self.__curBook = None

    def openFile(self, filePath):
        # 打开文件文件的初始化方法
        self.__initBook(filePath)
        if (self.__curBook is not None):
            rtn = self.__curBook.parseFile(filePath)
            if (rtn):
                self.__curPageNum = 1
            return rtn
        return False

    def closeFile(self):
        """关闭书籍文件"""
        print("关闭 " + self.__filePath + " 文件")
        return True

    def showCatalogue(self):
        """显示书籍目录的方法"""
        catalogue = self.__curBook.getCatalogue()
        catalogue.showInfo()

    def prePage(self):
        """上一页"""
        return self.gotoPage(self.__curPageNum - 1)

    def nextPage(self):
        """下一页"""
        return self.gotoPage(self.__curPageNum + 1)

    def gotoPage(self, pageNum):
        """转到指定目录"""
        if (pageNum < 1 or pageNum > self.__curBook.getPageCount()):
            return None
        self.__curPageNum = pageNum
        print("显示第" + str(self.__curPageNum) + "页")
        page = self.__curBook.getPage(self.__curPageNum)
        page.getContent()
        return page


###————————————————————————————以上是已经成熟的产品类——————————————————————###


class PdfAdapterBook(ThirdPdf, IBook):
    """pdf适配器:
        实现了接口类，并继承了第三方pdf类
    """
    def parseFile(self, filePath):
        # 模拟文档的解析
        rtn = super().open(filePath)
        if (rtn):
            print(filePath + "文件解析成功")
        return rtn

    def getCatalogue(self):
        outline = super().getOutline()
        print("将Outline结构的目录转换成Catalogue结构的目录")
        return Catalogue("PDF电子书")

    def getPageCount(self):
        return super().pageSize()

    def getPage(self, pageNum):
        page = self.page(pageNum)
        print("将PdfPage的面对象转换成Page的对象")
        return Page(page.getPageNum())


def testReader():

    reader = Reader("阅读器")

    if(not reader.openFile("平凡的世界.txt")):
        return
    reader.showCatalogue()
    reader.gotoPage(1)
    reader.nextPage()
    reader.closeFile()
    print()

    if(not reader.openFile("平凡的世界.epub")):
        return
    reader.showCatalogue()
    reader.gotoPage(5)
    reader.nextPage()
    reader.closeFile()
    print()

    if(not reader.openFile("平凡的世界.pdf")):
        return
    reader.showCatalogue()
    reader.gotoPage(10)
    reader.nextPage()
    reader.closeFile()


testReader()